<template>
  <div :bordered="false">
    <f-table ref="table" rowKey="customAddressId" :columns="columns" :multiple="false" :operator="operator" :autoload="false" :params="queryParam" :api="api.customAddress">
         <span slot="addressType" slot-scope="text, record, index">
            <a-tag color="#87d068" v-if="text==1">收货地址</a-tag>
            <a-tag color="#108ee9" v-if="text==2">发票收货地址</a-tag>
        </span>
      <span slot="state" slot-scope="text, record, index">
          <a-switch v-model="record.state==1" checkedChildren="是" unCheckedChildren="否" @change="(checked) => setState(checked,record)"/>
        </span>
      <span slot="action" slot-scope="text, record">
          <template>
            <a @click="edit(record)">编辑</a>
            <a-divider type="vertical"/>
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item v-if="$auth('table.delete')">
                <a href="javascript:;" @click="$refs.table.del(record)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
    </f-table>
    <custom-address-modal ref="addressModal" :custom-id="this.queryParam.customId" @handleSuccess="handleSuccess"></custom-address-modal>
  </div>
</template>

<script>
  import api from './js/api'
  import CustomAddressModal from './custom_address_modal'

  export default {
    name: 'CustomAddress',
    components: { CustomAddressModal },
    data() {
      let me = this
      //操作按钮
      let operator = []
      operator.push({
        name: '添加',
        icon: 'plus',
        that: this,
        handler: (table) => {
          this.$refs['addressModal'].showModal()
        }
      })
      return {
        api: Object.assign(api, this.api),
        //列表初始化查询参数
        queryParam: {
          customId: ''
        },
        // 表头
        columns: [
          {
            title: '#',
            width: '60',
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '地址类型',
            align: 'center',
            dataIndex: 'addressType',
            scopedSlots: { customRender: 'addressType' }
          },
          {
            title: '收货人',
            align: 'center',
            dataIndex: 'userName'
          },
          {
            title: '收货地址',
            align: 'center',
            dataIndex: 'address'
          },
          {
            title: '联系电话',
            align: 'center',
            dataIndex: 'telephone'
          },
          {
            title: '备注',
            align: 'center',
            dataIndex: 'remark'
          },
          {
            title: '状态',
            align: 'center',
            scopedSlots: { customRender: 'state' },
            dataIndex: 'state'
          },
          {
            title: '操作',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        operator: operator
      }
    },
    created() {
    },
    methods: {
      setData(data) {
        if (data && data.customId) {
          this.queryParam.customId = data.customId
          this.$refs['table'].refresh(false)
        }
      },
      //设置状态
      setState(check, record) {
        this.http.post(this, {
          url: this.api.customAddress.setState,
          data: {
            ids: record.customAddressId,
            state: Number(check)
          },
          success(me, data) {
            record.state = data
          }
        })
      },
      edit(record) {
        this.$refs['addressModal'].showModal(record)
      },
      handleSuccess() {
        this.$refs.table.refresh()
      }
    }
  }
</script>